#!/usr/bin/env python

# Copyright 2012 JapakGIS Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import webapp2
import os
import models
import datetime
import get
import getJson


from google.appengine.ext.webapp import template
from google.appengine.api import mail

class GadgetHandler(webapp2.RequestHandler):
    def get(self):
        now = datetime.datetime.now()
        date = {
                "Year": now.year
            }
        path = os.path.join(os.path.dirname(__file__), 'template/index.html')     
        self.response.cache_control.no_cache = None
        self.response.cache_control.public = True
        self.response.cache_control.max_age = 600
        self.response.out.write(template.render(path, date)) 

class MainHandler(webapp2.RequestHandler):
    def post(self):
        email = mail.EmailMessage(sender = "feedback@vote-iebc.appspotmail.com",
                                  subject="Gadget FeedBack")
        email.to = "david.japakgis@gmail.com"
        email.body = self.request.get("message")
        email.send()
        
    def get(self):
        now = datetime.datetime.now()
        date = {
                "Year": now.year
            }
        path = os.path.join(os.path.dirname(__file__), 'template/gadget.html')     
        self.response.cache_control.no_cache = None
        self.response.cache_control.public = True
        self.response.cache_control.max_age = 600
        self.response.out.write(template.render(path, date)) 



class GetData(webapp2.RequestHandler):    
        
    def get(self, rtype=False, reqname=False):
        filters = self.request.arguments()
        params = {}
        for f in filters:
            params["%s"%f]=self.request.get(f)
        
        if rtype in ["county", "constituency", "ward"]:
                self.response.headers['Content-Type'] = 'application/json'
                self.response.out.write(get.Locations("", rtype, params))
        elif rtype == "pollingstation":
            if reqname is not False:
                self.response.headers['Content-Type'] = 'application/json'
                self.response.out.write(get.PollingStationInfo(reqname))              
            else:
                self.response.headers['Content-Type'] = 'application/json'
                self.response.out.write(get.PollingStations(params))
        elif rtype == "datafeed":
            getJson.putData(params)     
            self.response.cache_control.no_cache = None
            self.response.cache_control.public = True
            self.response.cache_control.max_age = 600
            self.response.out.write("Okay")

                           
    
app = webapp2.WSGIApplication([
    (r'/', MainHandler),
    (r'/gadget/', GadgetHandler),
    (r'/(.*)/', GetData),
    (r'/(.*)/(.*)', GetData),
], debug=True)
